package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Category;
import org.example.pojo.Result;
import org.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类列表
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Category category){
        log.info("新增分类：{}", category);
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Category category){
        log.info("修改分类：{}", category);
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除分类：{}", id);
        categoryService.delete(id);
        return Result.success();
    }
}
